/**
 * https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/
 */
function sumRootToLeaf(root: TreeNode | null): number {
  const preOrder = (node: TreeNode | null, value: number): number => {
    if (node === null) {
      return 0;
    }

    value = (value << 1) | node.val;
    if (node.left === null && node.right === null) {
      return value;
    }

    return preOrder(node.left, value) + preOrder(node.right, value);
  };

  return preOrder(root, 0);
}
